﻿using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Iu.SignalR.Test.Controllers
{
    public class Chat : Hub
    {
        public async override Task OnConnectedAsync()
        {
            await this.Clients.All.InvokeAsync("Connected", this.Context.ConnectionId);
            await base.OnConnectedAsync();
        }

        public async override Task OnDisconnectedAsync(Exception exception)
        {
            await this.Clients.All.InvokeAsync("Disconnected", this.Context.ConnectionId);
            await base.OnDisconnectedAsync(exception);
        }

        public Task Send(string message)
        {
            return Clients.All.InvokeAsync("Send", this.Context.ConnectionId, message);
        }

        public Task SendObject(Message message)
        {
            return Clients.All.InvokeAsync("SendObject", new Message
            {
                Id = this.Context.ConnectionId,
                Content = message.Content,
            });

        }
    }

    public class Message
    {
        public string Id { get; set; }
        public string Content { get; set; }
    }
}
